<template>
	<view class="container">
		<video v-if="item.video && item.video != 0" :src="item.video" autoplay="true"></video>
		<image mode="widthFix" v-if="!item.video && item.ImgUrl" :src="item.ImgUrl"></image>
		<view style="padding: 0 10px;">
			<view class="between">
				<view>
					<text class="red">￥</text>
					<text class="red big-price font-size-20" style="padding: 0 10px 0 0;">{{ item.LastPrice }}</text>
					<text class="decoration">原价￥{{ item.GoodsPrice }}</text>
				</view>
				<view>已售<text class="red">{{ item.SaleCount }}</text>件</view>
			</view>
			<!-- 商品名称 -->
			<view class="font-size-20" style="padding: 10px 0;">{{ item.LongGoodsName }}</view>
			<!-- 描述 -->
			<view>{{ item.TjRemark }}</view>
			<!-- 优惠券-购买 -->
			<view class="around red-block" style="padding: 10px 0; margin: 10px 0;" @click="showDialog()">
				<view>
					<view class="font-size-20">{{ item.ActMoney }}元优惠券</view>
					<view>使用时间: {{ item.BeginDate | subStr10 }} - {{ item.EndDate | subStr10 }}</view>
				</view>
				<view>
					<view>立刻</view>
					<view>领券</view>
				</view>
			</view>
			<!-- 店铺 -->
			<view class="font-size-20">{{ item.ShopName }}</view>
			<!-- 图片 -->
			<image mode="widthFix" v-if="item.video" :src="item.ImgUrl"></image>
		</view>
		<!-- 提示框 -->
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="success" :showClose="false" confirmText="关闭" :content="content" @confirm="dialogClose"
				@close="dialogClose"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
export default {
	name: '',
	mixins: [],
	components: {},
	props: {},
	data() {
		return {
			// 商品详情
			item: {},
			// 淘口令/淘短链/跳转/弹窗对象
			goods: {},
			content: ''
		};
	},
	computed: {},
	watch: {},
	filters: {
		subStr10(e) {
			if (e) {
				return e.substr(0, 10);
			}
		}
	},
	onLoad(e) {
		let id = e.id || '596371055958';
		this.queryData(id);
	},
	methods: {
		// 淘口令—— tkl: ( CZ356 PD7teV3HWA6(:/  复制到剪贴板即可
		// 淘短链——shortLink: https://s.click.taobao.com/c53VEus   app环境用来唤醒淘宝（h5无法唤醒,微信也会限制）
		tbkjump() {
			let _that = this;
			uni.setClipboardData({
				data: this.goods.tkl,
				success() {
					// 隐藏默认
					uni.hideToast();
					_that.content = `已自动复制淘口令\n ${_that.goods.tkl} \n到剪贴板，请直接打开淘宝app使用`
					_that.$refs.alertDialog.open()
				},
				fail() {
					_that.content = `已生成商品淘口令\n ${_that.goods.tkl} \n请复制后打开淘宝app使用`
					_that.$refs.alertDialog.open()
				}
			});
		},
		dialogClose() {
			this.$refs.alertDialog.close()
		},
		// 查询数据
		async queryData(id) {
			await this.requestPromise(id)
				.then(res => {
					console.log('success', res, res.data.data[0]);
					this.item = res.data.data[0];
					console.log('商品详情', this.item);
				})
				.catch(e => {
					console.log('error', e);
				});
		},
		// 查询详情
		requestPromise(id) {
			return new Promise((resolve, reject) => {
				uni.request({
					url: 'http://api.xuandan.com/DataApi/item?AppKey=1yt5mfwx15&id=' + id,
					data: {},
					success: res => {
						resolve(res);
					},
					fail: e => {
						reject(e);
					},
					complete: () => {
						console.log('complete do');
					}
				});
			});
		},
		// 调用转链、展示淘口令
		async showDialog() {
			let id = this.item.GoodsId;
			await this.requestPromiseBuy(id)
				.then(res => {
					console.log('淘口令', res);
					this.goods = res.data;
					this.tbkjump()
				})
				.catch(e => {
					console.log('error', e);
				});
		},
		// 单品转链
		requestPromiseBuy(id) {
			return new Promise((resolve, reject) => {
				uni.request({
					url: 'http://api.xuandan.com/DataApi/GetItemTklLink?appkey=1yt5mfwx15&id=' + id,
					data: {},
					success: res => {
						resolve(res);
					},
					fail: e => {
						reject(e);
					},
					complete: () => {
						console.log('complete do');
					}
				});
			});
		},
	}
};
</script>

<style lang="less" scoped></style>
